package com.njtech.pet.controller;

import com.alibaba.fastjson.JSON;
import com.njtech.pet.constant.Constant;
import com.njtech.pet.entity.Action;
import com.njtech.pet.entity.Item;
import com.njtech.pet.service.ActionService;
import com.njtech.pet.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxj
 * Date 2020-04-01 18:21
 * Description Action控制层
 */
@Controller
@RequestMapping("/action")
public class ActionController {
    @Autowired
    ActionService actionService;

    @Autowired
    ItemService itemService;

    static final int MAX_COLLECT_NUM = 50;

    /**
     * 获取浏览记录
     *
     * @param uid 用户id
     * @return 封装了item的浏览记录
     */
    @RequestMapping(value = "/getViewHistory",method = RequestMethod.POST)
    @ResponseBody
    public String getViewHistory(@SessionAttribute(value = "uid") Integer uid) {
        if (uid == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(2);
        List<Action> items = actionService.getViewListByUserId(uid);
        map.put("items", items);
        return JSON.toJSONString(map);
    }

    /**
     * 获取收藏夹
     *
     * @param uid 用户id
     * @return 封装了item的列表
     */
    @RequestMapping(value = "/getCollection",method = RequestMethod.POST)
    @ResponseBody
    public String getCollection(@SessionAttribute(value = "uid") Integer uid) {
        if (uid == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(2);
        List<Action> items = actionService.getCollectListByUserId(uid);
        map.put("items", items);
        return JSON.toJSONString(map);
    }

    /**
     * 搜索记录
     *
     * @param uid 用户id
     * @return 字符串列表
     */
        @RequestMapping(value = "/getSearchHistory",method = RequestMethod.POST)
    @ResponseBody
    public String getSearchHistory(@SessionAttribute(value = "uid") Integer uid) {
        if (uid == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(2);
        List<String> searchList = actionService.getSearchListByUserId(uid);
        map.put("searchList", searchList);
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/addCollection",method = RequestMethod.POST)
    @ResponseBody
    public String addCollection(@SessionAttribute(value = "uid") Integer uid, Integer itemId) {

        Map<String, Object> map = new HashMap<>(2);
        if (checkItem(itemId, map)) {
            return JSON.toJSONString(map);
        }
        List<Action> actionList = actionService.getCollectListByUserId(uid);
        if (actionList.size() >= MAX_COLLECT_NUM) {
            map.put("status", 400);
            map.put("description", "收藏失败！收藏个数已达最大值！");
            return JSON.toJSONString(map);
        }

        int res = actionService.addCollectAction(uid, itemId);
        if (res == 0) {
            map.put("status", 500);
            map.put("description", "收藏失败！不存在的商品或重复的收藏请求");
        } else {
            map.put("status", 200);
            map.put("description", "收藏成功!");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/removeCollection", method = RequestMethod.POST)
    @ResponseBody
    public String removeCollection(@SessionAttribute(value = "uid")Integer uid, Integer itemId) {
        List<Action> actionList = actionService.getCollectListByUserId(uid);
        Map<String, Object> map = new HashMap<>(2);
        if (checkItem(itemId, map)) {
            return JSON.toJSONString(map);
        }
        for (Action action :
                actionList) {
            if (action.getItem().getId().equals(itemId)) {
                //定位到正常的,符合要求的action
                int row = actionService.removeAction(action.getId());
                if(row >0){
                    map.put("status", 200);
                    map.put("description", "取消成功！");
                    return JSON.toJSONString(map);
                }else{
                    map.put("status", 200);
                    map.put("description", "取消失败！");
                    return JSON.toJSONString(map);
                }

            }
        }
        map.put("status", 500);
        map.put("description", "非法请求！");
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/isCollected", method = RequestMethod.POST)
    @ResponseBody
    public String isCollected(@SessionAttribute(value = "uid")Integer uid, Integer itemId) {
        List<Action> actionList = actionService.getCollectListByUserId(uid);
        Map<String, Object> map = new HashMap<>(2);
        if (checkItem(itemId, map)) {
            return JSON.toJSONString(map);
        }
        for (Action action :
                actionList) {
            if (action.getItem().getId().equals(itemId)) {
                //定位到正常的,符合要求的action
                map.put("status", 200);
                map.put("res", true);
                return JSON.toJSONString(map);
            }
        }
        map.put("status", 200);
        map.put("res", false);
        return JSON.toJSONString(map);

    }

    @RequestMapping(value = "/addView", method = RequestMethod.POST)
    @ResponseBody
    public String addView(@SessionAttribute(value = "uid") Integer uid,Integer itemId) {
        Map<String, Object> map = new HashMap<>(2);
        if (checkItem(itemId, map)) {
            return JSON.toJSONString(map);
        }
        actionService.addViewAction(uid, itemId);
        if (checkItem(itemId, map)) {
            return JSON.toJSONString(map);
        }
        map.put("status", 200);
        map.put("res", actionService.getViewListByUserId(uid));
        return JSON.toJSONString(map);
    }

    private boolean checkItem(Integer itemId, Map<String,Object> map) {
        if (itemId == null) {
            map.put("status", 500);
            map.put("description", "参数错误！");
            return true;
        }
        Item checkItem = itemService.selectById(itemId);
        if (checkItem == null) {
            map.put("status", 500);
            map.put("description", "错误！不存在的商品");
            return true;
        }
        return false;
    }
}
